# -*- coding: utf-8 -*-
# -*- author: GXR -*-

import pymysql
import requests
from lxml import etree

db = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='1111',
    db='dingdian',
    charset='utf8'
)
cur = db.cursor()

response = requests.get(url='https://www.23us.so/files/article/html/13/13694/index.html')
response.encoding = response.apparent_encoding
html = etree.HTML(response.text)
for tr in range(1, 291):
    for td in range(1, 6):
        all_td = html.xpath("//table[@id='at']//tr[" + str(tr) + "]/td[@class='L'][" + str(td) + "]")
        for aa in all_td:
            title = aa.xpath("./a/text()")[0]
            href = aa.xpath("./a/@href")[0]

            sql = "insert shengxu values(1,'%s','%s')" % (title, href)
            cur.execute(sql)
            db.commit()
            print('%s=====完成:访问地址：%s' % (title, href))
cur.close()
db.close()
